<?php
/**
 * Form表单生成器
 */

namespace Todo\Builder;


use Think\Controller;

class FormBuilder extends Controller
{
    /**
     * 表单属性
     * @var array
     */
    protected $_attr = array(
        'action' => '',
        'class'  => 'ajax required-validate ',
        'enctype' => '',
        'method' => 'post',
        'name'   => '',
        'target' => '',
        'title'  => ''
    );

    /**
     * 表单数据
     * @var array
     */
    protected $_data = array();

    /**
     * 表单元素
     * @var array
     */
    protected $_items = array();

    protected $_style = '';

    protected $_type = 'add';

    public function setStyle($style=''){
        $this->_style = $style;
    }

    public function setType($type = 'edit'){
        $this->_type = $type;
    }

    /**
     * 设置表单属性
     * @param $atttName
     * @param string $attrVal
     */
    public function setAttr($atttName, $attrVal=''){
        $this->_attr[$atttName] = $attrVal;
    }

    /**
     * 加入一个表单项
     * @param $name 表单名
     * @param $type 表单类型(取值参考系统配置FORM_ITEM_TYPE)
     * @param $title 表单标题
     * @param $form 表单字段参数
     * @param $option 表单options
     * @return $this
     */
    public function addItem($name, $type, $title, $form = array(), $option = array()) {
        $item = array();
        $item['name'] = $name;
        $item['type'] = $type;
        $item['title'] = $title;
        $item['form'] = $form;
        $item['option'] = $option;
        $this->_items[] = $item;
        return $this;
    }

    /**
     * 表单赋值
     * @param $data
     */
    public function setData($data){
        $this->_data = $data;
    }

    /**
     * 渲染模板前赋值
     */
    public function display($tplFile = ''){
        $this->assign('attr', $this->_attr);
        foreach ($this->_items as &$item){
            if($this->_data && isset($this->_data[$item['name']])) $item['value'] = $this->_data[$item['name']];
            else if($item['form'] && isset($item['form']['default'])) $item['value'] = $item['form']['default'];
            else $item['value'] = '';
        }
        $this->assign('items', $this->_items);
        $this->assign('data', $this->_data);

        if(!$tplFile) $tplFile = 'Builder:'. ($this->_style ? $this->_style : 'default'). '_form';

        parent::display($tplFile);
    }

    public function customer(){
        $this->assign('attr', $this->_attr);
        $this->assign('type', $this->_type);
        $this->assign('items', $this->_items);
        return $this->fetch(APP_PATH .'../data/tpl/builder/customer_builder.html');
    }


    /**
     * 生成模板页面
     * @param string $viewFile 保存页面路径
     */
    public function builder($viewFile){
        $this->assign('attr', $this->_attr);
        $this->assign('type', $this->_type);
        foreach ($this->_items as &$item){
            if($this->_data && isset($this->_data[$item['name']])) $item['value'] = $this->_data[$item['name']];
            else if($item['form'] && isset($item['form']['default'])) $item['value'] = $item['form']['default'];
            else $item['value'] = '';
        }
        $this->assign('items', $this->_items);
        $this->assign('data', $this->_data);

        $tplHtml = file_get_contents(APP_PATH. '../data/tpl/adminlte/'.($this->_style ? $this->_style : 'admin').'_form.html');
        $formHtml =  $this->fetch(APP_PATH .'../data/tpl/builder/form_builder.html');
        file_put_contents($viewFile, str_replace('____CONTENT____', $formHtml, $tplHtml));
    }

    /**
     * 计算生成表单字段的样式和属性
     * @param array $column
     * @return array
     */
    public function generateForm($column){
        $form = array(
            'class' => $column['class'],
            'attr'  => $column['attr'],
            'default' => $column['default'],
            'help' => $column['help']
        );
        if($column['validation']){
            foreach ($column['validation'] as $k=>$v){
                switch ($k){
                    case 'minlength':
                        $form['attr'] .= ' minlength="' . $v . '"';
                        break;
                    case 'maxlength':
                        $form['attr'] .= ' maxlength="' . $v . '"';
                        break;
                    case 'min':
                        $form['attr'] .= ' maxlength="' . $v . '"';
                        break;
                    case 'max':
                        $form['attr'] .= ' maxlength="' . $v . '"';
                        break;
                    case 'remote':
                        $form['attr'] .= ' remote="' . C('API_BASE') . $v . '"';
                        break;
                    case 'required':
                        $form['class'] .= ' required';
                        break;
                    default:
                        $form['class'] .= ' ' . $k;
                        break;
                }
            }
        }
        if($column['type'] == 'date' && $form['default'] == 'now') $form['default'] = date(C('FORMAT.DATE'), NOW_TIME);
        if($column['type'] == 'datetime' && $form['default'] == 'now') $form['default'] = date(C('FORMAT.DATETIME'), NOW_TIME);
        return $form;
    }
}